import {ReactMarkDownEditor} from "../textEditor/ReactMarkDownEditor";

export interface IItem {
    title : string
    storeId : number
    abstract : string
    wordCount : number
}

export interface ISubListItem extends IItem{
    id : number
    isSelected : boolean
    isModificationMenuOpen : boolean
    getStoreIdSelf:()=>number
    deleteOwn: () => void | undefined
    onlyDisplayOwn: () => void | undefined
    onlyDisplayModificationMenuOwn: () => void | undefined,
    markDownEditor:ReactMarkDownEditor
}

export interface ISubListFactoryProps {
    anthologyTitle:string
}

export default class SubListFactory {
    private _list:ISubListItem[] = [];
    private id:number = 0;
    private readonly _anthologyTitle:string;

    constructor(props:ISubListFactoryProps){
        const {anthologyTitle} = props;
        this._anthologyTitle = anthologyTitle;
    }

    get list() {
        return this._list;
    }

    get anthologyTitle(): string {
        return this._anthologyTitle;
    }
    /**
     * storeId: 数据库id,用于向后端请求文章时发送
     * title 标题
     * abstract 概要
     * wordCount 字数
     * isSelected 是否被选中
     * isModificationMenuOpen 是否要打开修改菜单
     * deleteOwn 删掉自己
     * onlyDisplayOwn 修改isSelected,除了自己的isSelected为true,其余元素的为false
     * onlyDisplayModificationMenuOwn isModificationMenuOpen,除了自己的isModificationMenuOpen为true,其余元素的为false
     * */
    initSubListFactoryElement(val:IItem):ISubListItem{
        const id = this.id++;
        return {
            id,
            storeId : val.storeId,
            title : val.title,
            abstract : val.abstract,
            wordCount : val.wordCount,
            isSelected : false,
            isModificationMenuOpen : false,
            getStoreIdSelf:this.getStoreIdSelf(this._list, id),
            deleteOwn : this.deleteOwn(this._list, id),
            onlyDisplayOwn : this.forEachIsSelected(id),
            onlyDisplayModificationMenuOwn : this.forEachIsModificationMenuOpen(id),
            markDownEditor : new ReactMarkDownEditor({title:val.title,content:val.abstract,anthologyTitle:this._anthologyTitle})
        }
    }

    private deleteOwn(list:ISubListItem[],id:number){
        return function () {
            list.forEach((val,index)=>{
                if(val.id === id){
                    list.splice(index,1);
                }
            })
        }
    }

    /**
     * 会返回一个storeId,如果没有找到则返回-1
     * */
    private getStoreIdSelf(list:ISubListItem[],id:number){
        return function () {
            const LENGTH = list.length;
            for(let i=0;i<LENGTH;i++){
                if(list[i].id === id){
                    return list[i].storeId
                }
            }
            return -1;
        }
    }

    /**
     * 将所有元素的isModificationMenuOpen修改为false
     * */
    noDisplayModificationMenu(){
        this._list.forEach((val)=>{
            val.isModificationMenuOpen = false;
        })
    }

    private forEachIsSelected(id:number){
        return ()=>{
            this._list.forEach((val)=>{
                val.isSelected = val.id === id;
            })
        }
    };

    private forEachIsModificationMenuOpen(id:number){
        return ()=>{
            this._list.forEach((val)=>{
                val.isModificationMenuOpen = val.id === id;
            })
        }
    };

    push(val:IItem):ISubListItem[]{
        this._list.push(this.initSubListFactoryElement(val));
        return this._list;
    }

    unshift(val:IItem):ISubListItem[]{
        this._list.unshift(this.initSubListFactoryElement(val));
        return this._list;
    }

    splice(index:number):ISubListItem[]{
        this._list.splice(index, 1);
        return this._list;
    }

    /**
     * 清理列表
     * */
    clearList(){
        this._list = [];
    }


    /**
     * 将其他类型的数组转换为IITem类型的数组
     * */
    extra(data:any):IItem[]{
        const LENGTH = data.length;
        let _data:IItem[] = [];
        for(let i=0;i<LENGTH;i++){
            const {title, id, abstract='', wordCount=0} = data[i];
            _data[i] = {
                title:title,
                storeId:id,
                abstract:abstract,
                wordCount:wordCount
            }
        }
        return _data;
    }

    /**
     * 重新设置list
     * @param data{IItem[]}
     * */
    againSetList(data:IItem[]):ISubListItem[]{
        this.clearList();
        const LENGTH = data.length;
        for(let i=0;i<LENGTH;i++){
            this.push(this.initSubListFactoryElement(data[i]));
        }
        return this._list;
    }

    /**
     * 找出被选中的元素的索引
     * */
    findSelectedIndex():number{
        for(let i = 0, length = this._list.length;i<length;i++){
            if(this._list[i].isSelected){
                return i;
            }
        }
        return -1;
    }

}